package com.suixun.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.authority.controller.BaseManager;
import com.google.gson.Gson;
import com.suixun.service.TeacherService;
import com.suixun.util.Constant;
import com.suixun.vo.TeacherVO;
@Controller
@RequestMapping(value = "/teacherManage")
public class TeacherController extends BaseManager {
	
	@Resource
	private TeacherService teacherService;
	
	/**
	 * 分页获取教师信息
	 * 
	 * @return
	 */

	@RequestMapping(value = "/getTeachers.json")
	@ResponseBody
	public Object getTeachers(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		TeacherVO teacherVo = null;
		try {
			if (param == null) {
				teacherVo = new TeacherVO();
			}else {
				teacherVo = new Gson().fromJson(param, TeacherVO.class);
			}
			return success(teacherService.getTeachersPage(teacherVo));
		} catch (Exception e) {
			logger("TeacherController getTeachers:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}
	
	/**
	 * 
	 * <p>Title: exportTamplate</p>
	 * <p> 功能描述:导入excel模板下载 </p>
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/export_TeacherTemplate.json")
	@ResponseBody
	public Object exportTeacherTemplate() {
		try {
			ArrayList<String> listParm = new ArrayList<String>();
			ArrayList<String> listExample = new ArrayList<String>();
			HSSFWorkbook workbook=null;
			List<List<String>> listValue = new LinkedList<List<String>>();
			listParm.add(Constant.Excel.GRADE);
			listParm.add(Constant.Excel.TEACHERNAME);
			listParm.add(Constant.Excel.TELEPHONE);
			listParm.add(Constant.Excel.SCHOOL);
			listParm.add(Constant.Excel.CARD_SEND_COUNT);
			// 创建新的Excel 工作簿
			workbook = exportDeviceList(Constant.Excel.TEACHER_EXCEL, listParm,listExample, listValue);
			HttpServletResponse response = this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=teacherTemplate.xls");
			workbook.write(this.getResponse().getOutputStream());
		} catch (Exception e) {
			logger("TeacherController exportTeacherTemplate:"+e.getMessage(),e);
			toRedirect(500);
		}
		return success();
	}
	
	
	/**
	 * 
	 * <p>Title: queryAllDeviceRows</p>
	 * <p> 功能描述:导入教师信息 </p>
	 * @return
	 */
	@RequestMapping(value = "/addTeachers.json", method = RequestMethod.POST)
	@ResponseBody
	public Object addTeachers(@RequestParam("file") MultipartFile file,HttpServletRequest request){
		boolean bln=checkRight();
		if(!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		InputStream fileReport = null;
		try {
			fileReport=file.getInputStream();
			String fileName = file.getOriginalFilename();
			if (fileName.endsWith("xlsx")) {
				return fail(Constant.Excel.TYPE_IS_FALSE);
			}
			//把一张xls的数据表读到wb里
			HSSFWorkbook wb = new HSSFWorkbook(fileReport);
			Object isImport=teacherService.importTeachers(wb);
			if (isImport instanceof String) {
				String res = (String)isImport;
				if (Constant.SUCCESS.equals(res)) {
					return success();
				}else{
					return fail(res);
				}
			}else {
				return success(isImport);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger("TeacherController addTeachers:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}finally{
			try {
				fileReport.close();
			} catch (IOException e) {
				logger("TeacherController addTeachers:"+e.getMessage(),e);
			}
		}
	}
	
	/**
	 * 修改教师信息
	 * 
	 * @return
	 */

	@RequestMapping(value = "/editTeacher.json")
	@ResponseBody
	public Object editTeacher(String param) {
		if (!checkRight()) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			TeacherVO teacherVo = checkParam(param,TeacherVO.class);
			String res = teacherService.editTeacher(teacherVo);
			if (Constant.SUCCESS.equals(res)) {
				return success();
			}else {
				return fail(res);
			}
		} catch (Exception e) {
			logger("TeacherController editTeacher:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
	
	/**
	 * 删除教师信息
	 * 
	 * @return
	 */

	@RequestMapping(value = "/delTeacher.json")
	@ResponseBody
	public Object delTeacher(String param) {
		if (!checkRight()) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		try {
			TeacherVO teacherVo = checkParam(param,TeacherVO.class);
			teacherVo.setIsdelete(1);
			String res = teacherService.editTeacher(teacherVo);
			if (Constant.SUCCESS.equals(res)) {
				return success();
			}else {
				return fail(res);
			}
		} catch (Exception e) {
			logger("TeacherController delTeacher:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
}
